﻿Imports System.Data.SqlClient

Public Class frmUsuarios
    Dim Us As New clsUsuarios
    Dim Pe As New clsPermisosUsuario

    Private Sub frmUsuarios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaNombres()
        cargaPermisos()

    End Sub
    Sub CargaNombres()
        Dim cnconn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String
        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "Select NombreCompleto FROM Empleados WHERE EstadoEmpleado<>'B'"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                txtNombre.AutoCompleteCustomSource.Add(rdBuscar("NombreCompleto").ToString.Trim)

            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try

    End Sub
    Sub BuscaDatosUsuario()
        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = InitialConnection

        Dim strSql As String = "SELECT * FROM Usuarios WHERE UPPER(Nombre)='" & UCase(txtUsuario.Text.Trim) & "'"

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn
        cmdBuscar.CommandText = strSql

        Dim rdBuscar As SqlDataReader
        cnconn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            txtPassWord.Text = rdBuscar("Password").ToString.Trim
            txtNombre.Text = rdBuscar("NombreCompleto").ToString.Trim
            txtPasswordConfirmacion.Text = rdBuscar("Password").ToString.Trim
        End If

        CargaPermisosdeUsuario()
       
    End Sub
    Sub cargaPermisos()
        lstOpciones.Items.Clear()

        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = InitialConnection

        Dim strSql As String = "Select Descripcion from Formularios Order By Familia"

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn
        cmdBuscar.CommandText = strSql

        Dim rdBuscar As SqlDataReader
        cnconn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            lstOpciones.Items.Add(rdBuscar("Descripcion").ToString.Trim)
        Loop
    End Sub
    Sub CargaPermisosdeUsuario()
        dgPermisos.Rows.Clear()

        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = InitialConnection

        Dim Usuario As Integer = BuscarIntDatoInicial("Usuarios", "IdUsuario", "Nombre='" & UCase(txtUsuario.Text.Trim) & "'")

        Dim strSql As String = "Select * FROM PermisosUsuarios WHERE IdUsuario=" & Usuario

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn
        cmdBuscar.CommandText = strSql

        Dim rdBuscar As SqlDataReader
        cnconn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            dgPermisos.Rows.Add(rdBuscar("IdPermiso"), Trim(BuscarStrDatoInicial("Formularios", "Descripcion", "IdFormulario=" & rdBuscar("IdFormulario"))))
        Loop
    End Sub

    Private Sub toolGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolGuardar.Click
        If txtUsuario.Text = "" Then
            MsgBox("Debes indicar un nombre de usuario", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtNombre.Text = "" Then
            MsgBox("Debes indicar el nombre completo del usuario", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtNombre.Text = "" Then
            MsgBox("Debes indicar el nombre completo del usuario", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        If txtPassWord.Text = "" Then
            MsgBox("Debes indicar una contraseña para el usuario", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtPassWord.Text <> txtPasswordConfirmacion.Text Then
            MsgBox("Las contraseñas no coinciden", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim IdUsuario As Integer = BuscarIntDatoInicial("Usuarios", "IdUsuario", "UPPER(Nombre)='" & UCase(txtUsuario.Text.Trim) & "'")
        If IdUsuario = 0 Then
            Us.IdUsuario = (BuscarIntDatoInicial("SELECT MAX(IdUsuario) as Id FROM Usuarios")) + 1
            Us.Nombre = txtUsuario.Text.Trim
            Us.NombreCompleto = txtNombre.Text.Trim
            Us.PassWord = txtPassWord.Text.Trim
            Us.Insertar()
        Else
            Us.IdUsuario = IdUsuario
            Us.Nombre = txtUsuario.Text.Trim
            Us.NombreCompleto = txtNombre.Text.Trim
            Us.PassWord = txtPassWord.Text.Trim
            Us.Actualizar()
        End If

        Panel1.Enabled = True

    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click

    End Sub

    Private Sub toolSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolSalir.Click
        Close()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        If UCase(txtUsuario.Text.Trim) = UCase(usuarioactivo) Then
            MsgBox("No se puede eliminar el usurio actual", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        Dim IdUsuario As Integer = BuscarIntDatoInicial("Usuarios", "IdUsuario", "UPPER(Nombre)='" & UCase(txtUsuario.Text.Trim) & "'")
        Us.IdUsuario = IdUsuario
        Us.Borrar()
    End Sub

    Private Sub btnAbrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbrir.Click
        Dim IdUser As Integer = BuscarIntDatoInicial("Usuarios", "IdUsuario", "UPPER(Nombre)='" & UCase(txtUsuario.Text.Trim) & "'")
        Dim IdFrm As Integer = BuscarIntDatoInicial("Formularios", "IDFormulario", "Descripcion='" & lstOpciones.Text.Trim & "'")
        Pe.IdUser = IdUser
        Pe.IdFrm = IdFrm
        Pe.Permiso = 1
        Pe.Insertar()
        CargaPermisosdeUsuario()
    End Sub

    Private Sub RadButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton2.Click
        Dim IdSelect = dgPermisos.CurrentRow.Cells(0).Value

        Pe.Id = IdSelect
        Pe.Borrar()

        CargaPermisosdeUsuario()

    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        txtNombre.Clear()
        txtPassWord.Clear()
        txtPasswordConfirmacion.Clear()
        txtUsuario.Clear()
        dgPermisos.Rows.Clear()

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim strResult As String
        Dim fBusqueda As New frmUsuariosListado

        fBusqueda.ShowDialog()

        If fBusqueda.DialogResult = Windows.Forms.DialogResult.OK Then
            strResult = fBusqueda.Valor

            If strResult <> "" Then
                txtUsuario.Text = strResult
                BuscaDatosUsuario()
            End If
        End If
    End Sub

    Private Sub txtUsuario_Leave(sender As Object, e As System.EventArgs) Handles txtUsuario.Leave
        BuscaDatosUsuario()
    End Sub

    Private Sub txtUsuario_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtUsuario.TextChanged

    End Sub
End Class